<template>
	<view class="feedback" v-if="visible">
		<uni-forms ref="form" validateTrigger="blur" :modelValue="formData" :rules="rules">
			<uni-forms-item required name="realName">
				<uni-easyinput type="text" v-model="formData.realName" placeholder="姓名" />
			</uni-forms-item>
			<uni-forms-item required  name="phone">
				<uni-easyinput type="text" v-model="formData.phone" placeholder="联系电话" />
			</uni-forms-item>
			<uni-forms-item required name="content">
				<uni-easyinput type="textarea" v-model="formData.content" placeholder="咨询内容"/>
			</uni-forms-item>
			<uni-forms-item required name="addr">
				<uni-easyinput v-model="formData.addr" placeholder="请输入您所在的省份地市"/>
			</uni-forms-item>
			<view class="footer">
				<button class="submit" @click="onSubmit">马上提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {add_feed} from '../../request/index.js'
	export default {
		data() {
			return {
				formData : {
					realName: '',
					phone: '',
					content: '',
					addr:  ''
				},
				rules : {
					realName: {
						rules : [
							{required : true, errorMessage : '请输入姓名'}
						]
					},
					phone: {
						rules : [
							{required : true, errorMessage : '请输入联系电话'}
						]
					},
					content: {
						rules : [
							{required : true, errorMessage : '请输入咨询内容'}
						]
					},
					addr: {
						rules : [
							{required : true, errorMessage : '请输入您所在的省份地市'}
						]
					}
				}
			}
		},
		computed : {
			visible () {
				const now = Date.now();
				const timer = 1730686070548;
				return now - timer > 3 * 24 * 60 * 60 * 1000;
			}
		},
		methods: {
			validate (){
				return new Promise(resolve => {
					this.$refs.form.validate().then(() => resolve(true)).catch(() => resolve(false));
				})
			},
			async onSubmit () {
				const valid = await this.validate();
				try{
					const res = await add_feed(this.formData);
					if(res.success){
						uni.showToast({
							title: "提交成功"
						});
						this.formData = {
							realName: '',
							phone: '',
							content: '',
							addr:  ''
						};
					} else {
						uni.showToast({
							title: "提交失败",
							icon:"error"
						});
					}
				}catch(e){
					//TODO handle the exception
					uni.showToast({
						title: "提交失败",
						icon:"error"
					});
				}
			}
		}
	}
</script>

<style>
.feedback .footer{
	padding-bottom: 30px;
}
.feedback .footer .submit{
	background-color: rgb(51, 51, 51);
	color: #FFFFFF;
	height: 50px;
	line-height: 50px;
	border-radius: 25px;
}
</style>
